package com.demo.maintenance.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.demo.maintenance.R;
import com.demo.maintenance.bean.ChildBean;
import com.demo.maintenance.contant.Global;
import com.demo.maintenance.utils.AntiShakeUtils;
import com.demo.maintenance.utils.HttpManager;
import com.demo.maintenance.utils.SystemBarTintManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;

public class StudentListActivity extends AppCompatActivity {

    private RecyclerView recycler_view;
    private ListAdapter listAdapter;
    private List<ChildBean.Data> datasource = new ArrayList<>();
    private FloatingActionButton btn_create;

    @Override
    protected void onResume() {
        super.onResume();
        requestData();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        QMUIStatusBarHelper.translucent(this);
        SystemBarTintManager.setStatusBarDarkTheme(this, true);
        setContentView(R.layout.activity_list_student);

        findViewById(R.id.common_back_iv).setOnClickListener(v -> finish());
        ((TextView) findViewById(R.id.common_content_tv)).setText("Child List");

        listAdapter = new ListAdapter();
        recycler_view = findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        recycler_view.setAdapter(listAdapter);
    }

    private void requestData() {

        WaitDialog.show("Waiting");

        String url = Global.ipAddress + "/system/child_info/listAll?teacherPhone=" + Global.userResponse.data.userPhone;

        try {
            HttpManager.get(url, new HttpManager.NetworkCallBack() {
                @Override
                public void onSuccess(String successInfo) {
                    try {
                        Log.e("monitoring", successInfo);

                        ChildBean assignmentBean = JSON.parseObject(successInfo, ChildBean.class);
                        if (assignmentBean.code == 200) {
                            runOnUiThread(() -> {
                                if (assignmentBean.rows != null && !assignmentBean.rows.isEmpty()) {
                                    datasource.clear();
                                    datasource.addAll(assignmentBean.rows);
                                    listAdapter.notifyDataSetChanged();
                                }
                            });
                        } else {

                            Log.e("monitoring", "get list failed !");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(String errorInfo) {
                    Log.e("monitoring", errorInfo);
                }

                @Override
                public void onFinal() {
                    super.onFinal();
                    WaitDialog.dismiss();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            WaitDialog.dismiss();
        }
    }

    private class ListAdapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(StudentListActivity.this)
                    .inflate(R.layout.recycler_item_student, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            ChildBean.Data item = datasource.get(position);
            holder.tv_student_name.setText(item.childName);
            if ("0".equals(item.childSex)){
                holder.iv_gender.setImageResource(R.mipmap.boy);
            }else {
                holder.iv_gender.setImageResource(R.mipmap.girl);
            }
            holder.itemView.setOnClickListener(v -> {
                if (AntiShakeUtils.isInvalidClick(v)){
                    return;
                }
                Intent intent = new Intent(StudentListActivity.this, CourseListActivity.class);
                intent.putExtra("childId", item.childId);
                intent.putExtra("childSex", item.childSex);
                intent.putExtra("childName", item.childName);
                intent.putExtra("childClass", item.childClass);
                startActivity(intent);
            });
        }

        @Override
        public int getItemCount() {
            return datasource.size();
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_student_name;
        public ImageView iv_gender;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tv_student_name = itemView.findViewById(R.id.tv_student_name);
            iv_gender = itemView.findViewById(R.id.iv_gender);

        }
    }
}
